﻿using Batman.Cms.Core.Services;
using Batman.Jianbei.Forum.Core.Domains.Users;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Batman.Jianbei.Forum.Services.Weixins
{
    public class WeixinUserManager : BaseManager<WeixinUser>
    {
        /// <summary>
        /// 根据openId判断数据库中是否存在，不存在则写入
        /// </summary>
        /// <param name="weixinUser"></param>
        /// <returns></returns>
        public WeixinUser GetOrCreate(WeixinUser weixinUser)
        {
            var query = Repository.GetAll().AsNoTracking();

            var existsing = query.FirstOrDefault(t => t.OpenId == weixinUser.OpenId);

            if (existsing == null)
            {
                Repository.Insert(weixinUser);
                return weixinUser;
            }
            else
                return existsing;
        }
    }
}
